﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HotelManagement._04.OBJECT;
using HotelManagement._02.BUS;

namespace HotelManagement._01.FORM
{
    public partial class F42_CustomerService : Form
    {
        public F42_CustomerService()
        {
            InitializeComponent();
            Init();
            LoadDataToGrid();
            if (AssetsGS.instance.RoleUser == "User") btnDelete.Enabled = false;
        }
        CustomerService m_CustomerService;
        CustomerServiceBUS m_CustomerServiceBUS;

        private void Init()
        {
            m_CustomerService = new CustomerService();
            m_CustomerServiceBUS = new CustomerServiceBUS();
            ServiceBUS m_ServiceBUS = new ServiceBUS();
            m_cbb_ServiceID.DataSource = m_ServiceBUS.ViewService();
            m_cbb_ServiceID.DisplayMember = "ServiceDescription";
            m_cbb_ServiceID.ValueMember = "ServiceID";
        }
        private void LoadDataToGrid()
        {
            m_dgv_CustomerService.DataSource = m_CustomerServiceBUS.ViewCustomerService();
        }
        private CustomerService GetDataFromForm()
        {
            CustomerService v_CustomerService = new CustomerService();
            v_CustomerService.CustomerID =int.Parse( m_txt_CustomerID.Text);
            v_CustomerService.ServiceID = int.Parse(m_cbb_ServiceID.SelectedValue.ToString());
            v_CustomerService.UsingDate = m_dt_UsingDate.Value;
            if (CValidate.IsNumber(m_txt_CustomerServiceID.Text)) v_CustomerService.CustomerServiceID = int.Parse(m_txt_CustomerServiceID.Text);
            return v_CustomerService;
        }
        private void SetDataToForm(CustomerService ip_CustomerService)
        {
            CustomerBUS v_CustomerBUS = new CustomerBUS();
            m_txt_CustomerID.Text = ip_CustomerService.CustomerID.ToString();
            m_txt_CustomerName.Text = v_CustomerBUS.ViewCustomerById(ip_CustomerService.CustomerID).Rows[0]["CustomerName"].ToString(); ;
            m_dt_UsingDate.Value = ip_CustomerService.UsingDate;
            m_cbb_ServiceID.SelectedValue = ip_CustomerService.ServiceID;
            m_txt_CustomerServiceID.Text = ip_CustomerService.CustomerServiceID.ToString();
        }
        private void ResetText()
        {
            m_txt_CustomerID.Text = "";
            m_txt_CustomerName.Text = "";
            m_dt_UsingDate.Value = DateTime.Now;
            m_txt_CustomerServiceID.Text = "";
            m_txt_RoomID.Enabled = true;
        }
        private bool ValidateForm()
        {
            if (CValidate.IsNullString(m_txt_CustomerName.Text)) { MessageBox.Show("Bạn chưa nhập tên khách hàng !"); return false; }
            return true;

        }
        private void Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void Add_Click(object sender, EventArgs e)
        {
            if (m_txt_CustomerServiceID.Text.Trim() != "") return;
            if (!ValidateForm()) return;
            try 
	        {   
                m_CustomerService = GetDataFromForm();
                m_CustomerService.Status = "UnPaid";
                if (m_CustomerServiceBUS.AddCustomerService(m_CustomerService)) MessageBox.Show("Thêm dịch vụ khách hàng thành công");
	        }
	        catch (Exception v_e)
	        {
		        throw v_e;
	        }
            ResetText();
            LoadDataToGrid();
        }

        private void Update_Click(object sender, EventArgs e)
        {
            if (m_txt_CustomerServiceID.Text.Trim() == "") return;
            if (!ValidateForm()) return;
            try
            {
                m_CustomerService = GetDataFromForm();
                m_CustomerService.Status = "UnPaid";
                if (m_CustomerServiceBUS.UpdateCustomerService(m_CustomerService)) MessageBox.Show("Sửa dịch vụ khách hàng thành công");
            }
            catch (Exception v_e)
            {
                throw v_e;
            }
            ResetText();
            LoadDataToGrid();
        }

        private void m_dgv_CustomerService_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                m_CustomerService.CustomerServiceID =int.Parse(m_dgv_CustomerService.Rows[m_dgv_CustomerService.CurrentCell.RowIndex].Cells["CustomerServiceID"].Value.ToString());
                m_CustomerService.CustomerID = int.Parse(m_dgv_CustomerService.Rows[m_dgv_CustomerService.CurrentCell.RowIndex].Cells["CustomerID"].Value.ToString());
                m_CustomerService.ServiceID = int.Parse(m_dgv_CustomerService.Rows[m_dgv_CustomerService.CurrentCell.RowIndex].Cells["ServiceID"].Value.ToString());
                m_CustomerService.UsingDate =DateTime.Parse(m_dgv_CustomerService.Rows[m_dgv_CustomerService.CurrentCell.RowIndex].Cells["UsingDate"].Value.ToString());
                SetDataToForm(m_CustomerService);
                m_txt_RoomID.Enabled = false;
            }
            catch (Exception)
            {

                MessageBox.Show("Bạn chưa chọn loại dịch vụ nào !");
            }
        }

        private void Delete_Click(object sender, EventArgs e)
        {
            if (m_txt_CustomerServiceID.Text.Trim() == "") return;
            if (MessageBox.Show("Bạn có chắc là bạn muốn xóa thông tin này ?", "Xác nhận", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
            {
                if (m_CustomerServiceBUS.DeleteCustomerService(m_CustomerService)) MessageBox.Show("Đã xóa thành công !"); else MessageBox.Show("Lỗi không thể xóa !");
                LoadDataToGrid();
            }
            ResetText();
        }

        private void Reset(object sender, EventArgs e)
        {
            ResetText();
        }

        private void Search_Click(object sender, EventArgs e)
        {
            CheckInInfoBUS v_CheckInInfoBUS = new CheckInInfoBUS();
            DataTable v_dt = new DataTable();
            v_dt = v_CheckInInfoBUS.ViewInfoRoom(m_txt_RoomID.Text);
            if (v_dt.Rows.Count == 0)
            {
                MessageBox.Show("Phòng này hiện chưa có người sử dụng !");
                return;
            }
            else
            {
                CustomerBUS v_CustomerBUS = new CustomerBUS();
                m_txt_CustomerID.Text = v_dt.Rows[0]["CustomerID"].ToString();
                m_txt_CustomerName.Text = v_CustomerBUS.ViewCustomerById(int.Parse(v_dt.Rows[0]["CustomerID"].ToString())).Rows[0]["CustomerName"].ToString();
            }

        }

        private void btnDelete_Click(object sender, EventArgs e)
        {

        }

        private void Cancel_CLick(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
